package com.rem.rabbitmq.ee.Cack;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rem.rabbitmq.ee.RabbitMqUtils;

import java.util.concurrent.TimeUnit;


/**
 *
 * @author Rem
 * @date 2021-12-23
 */

public class Consumer03 {

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqUtils.getChannel();
        //消费回调
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("接收到的消息：" + new String(message.getBody()));

            try {
                //休眠10s
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /**
             * DeliveryTag – 来自收到的AMQP.Basic.GetOk或AMQP.Basic.Deliver的标签
             * 多个 – true 确认所有消息，包括提供的交付标签； false 仅确认提供的交付标签
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };

        //取消消费回调 如果在消费的时候队列被删除了
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("消费消息被中断");
        };
        System.out.println("消费者03 等待消费");

        //手动应答
        boolean autoAck = false;
        channel.basicConsume(RabbitMqUtils.QUEUE_ACK, autoAck, deliverCallback, cancelCallback);
    }
}
